<?php

namespace App\Http\Controllers\api;

use App\AddressInfo;
use App\Banner;
use App\Http\Services\FileManager;
use App\Notice;
use http\Env\Response;
use Illuminate\Http\Request;
use App\JoyInfo;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class OtherController extends Controller
{

    public function goToUrl(Request $request){
        $url = $request->get("url");
        return redirect()->to($url);
    }

    public function getBannerList (Request $request)
    {
        $allBanner = Banner::orderBy("sort", "asc")->orderBy("created_at", "desc")->get();
        if (count($allBanner) > 0) {
            foreach ($allBanner as $banner) {
                $banner->photo_url = FileManager::getImgFromOssUrl($banner->img_id);
            }
        }
        return response()->json(['level' => 'success', 'message' => '获取成功', 'data' => $allBanner]);
    }

    public function getJoyInfoList (Request $request)
    {
        $allJoyInfo = JoyInfo::orderBy('sort', 'asc')->orderBy('created_at', 'desc')->get();
        if (count($allJoyInfo) > 0) {
            foreach ($allJoyInfo as $JoyInfo) {
                $JoyInfo->img_url = FileManager::getImgFromOssUrl($JoyInfo->img_id);
            }
        }

        return response()->json(['level' => 'success', 'message' => '获取成功', 'data' => $allJoyInfo]);
    }


    public function getNoticeList (Request $request)
    {


        $notices = Notice::orderBy('created_at', 'desc')->get();


        return response()->json(['level' => 'success', 'message' => '获取成功', 'data' => $notices]);
    }


    public function getNotice (Request $request)
    {

        $id = $request->get('notice_id');

        $thatNotice = Notice::whereId($id)->first();
        $thatNotice->notice_content = html_entity_decode($thatNotice->content);
        return response()->json(['level' => 'success', 'message' => '获取成功', 'data' => $thatNotice]);

    }

    public function getAddress(Request $request)
    {
        $name = $request->get('name');
        $city     = $request->get('city');
        $area     = $request->get('area');

        if(!is_null($name) && trim($name) != ''){
            $findAddress=AddressInfo::where("province","=",$name)->orWhere("city","=",$name)->orWhere("area","=",$name)->first();

            if(!$findAddress){
                $pid=-1;
            }else{
                $pid=$findAddress->id;
            }
        }else{
            $pid=0;
        }

        $addressList=AddressInfo::wherePid($pid)->get();

        $addressInfos = array();
        if(count($addressList) > 0){
            foreach ($addressList as $addressInfo){
                $addressName = "";
                if(!is_null($addressInfo->province) && trim($addressInfo->province) != ''){
                    $addressName = $addressInfo->province;
                }
                if(!is_null($addressInfo->city) && trim($addressInfo->city) != ''){
                    $addressName = $addressInfo->city;
                }
                if(!is_null($addressInfo->area) && trim($addressInfo->area) != ''){
                    $addressName = $addressInfo->area;
                }
                if(!is_null($addressInfo->town) && trim($addressInfo->town) != ''){
                    $addressName = $addressInfo->town;
                }

                array_push($addressInfos,$addressName);
            }
        }


        return response()->json(['level' => 'success', 'message' => '获取成功', 'data' =>$addressInfos]);
    }

    public function getAddressAll(Request $request){
        $provinceInfos = AddressInfo::wherePid(0)->get();

        $resultInfo = array();
        if(count($provinceInfos) > 0){
            foreach ($provinceInfos as $provinceInfo){
                $p["name"] = $provinceInfo->province;
                $p["cityList"] = array();

                /** @var AddressInfo $provinceInfo */
                $cityInfos = $provinceInfo->children;
                if($cityInfos && count($cityInfos) > 0){
                    foreach ($cityInfos as $cityInfo){
                        $c["name"] = $cityInfo->city;
                        $c["areaList"] = array();

                        $areaInfos = $cityInfo->children;
                        if($areaInfos && count($areaInfos) > 0){
                            foreach ($areaInfos as $areaInfo){
                                if(!is_null($areaInfo->area)){
                                    $a["name"] = $areaInfo->area;
                                    $a["townList"] = array();

                                    $townInfos = $areaInfo->children;
                                    if($townInfos && count($townInfos)>0){
                                        foreach ($townInfos as $townInfo){
                                            array_push($a["townList"],$townInfo->town);
                                        }
                                    }

                                    array_push($c["areaList"],$a);
                                }else{
                                    array_push($c["areaList"],$areaInfo->town);
                                }

                            }
                        }

                        array_push($p["cityList"],$c);
                    }
                }
                array_push($resultInfo,$p);
            }
        }

        return response()->json(["level"=>"success","message"=>"获取成功","data"=>$resultInfo]);
    }

    public function getAddressAllToTown(Request $request){
        $provinceInfos = AddressInfo::whereNotNull("city")->whereNull("area")->get();

        $resultInfo = array();
        if(count($provinceInfos) > 0){
            foreach ($provinceInfos as $provinceInfo){
                $p["name"] = $provinceInfo->city;
                $p["cityList"] = array();

                /** @var AddressInfo $provinceInfo */
                $cityInfos = $provinceInfo->children;
                if($cityInfos && count($cityInfos) > 0){
                    foreach ($cityInfos as $cityInfo){
                        $c["name"] = $cityInfo->area;
                        $c["areaList"] = array();

                        $areaInfos = $cityInfo->children;
                        if($areaInfos && count($areaInfos) > 0){
                            foreach ($areaInfos as $areaInfo){
                                if(!is_null($areaInfo->town)){
                                    $a["name"] = $areaInfo->town;
//                                    $a["townList"] = array();
//
//                                    $townInfos = $areaInfo->children;
//                                    if($townInfos && count($townInfos)>0){
//                                        foreach ($townInfos as $townInfo){
//                                            array_push($a["townList"],$townInfo->town);
//                                        }
//                                    }

                                    array_push($c["areaList"],$a);
                                }else{
                                    array_push($c["areaList"],$areaInfo->town);
                                }

                            }
                        }

                        array_push($p["cityList"],$c);
                    }
                }
                array_push($resultInfo,$p);
            }
        }

        return response()->json(["level"=>"success","message"=>"获取成功","data"=>$resultInfo]);
    }
}
